﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using DBService;
using DB2Service;

namespace RXCourt
{
    public class SQLStringService
    {

        public enum FyType
        {
            UnKnown,
            Standard,
            Speciel_LiuZhou
        }
        private static FyType CurrentFyType = FyType.UnKnown;
        public static string GetSelectStr_TSJG()
        {
            CheckFyType();
            string s1 = "SELECT * FROM B_TSJG WHERE SFTJFLAG=1 ORDER BY TSPX ASC";
            string s2 = "SELECT B_TSJG.*,B_TSJG.JGBS as XH  FROM B_TSJG WHERE YWBM=1 ORDER BY MC";

            switch (CurrentFyType)
            {
                case FyType.Standard:
                    return s1;
                case FyType.Speciel_LiuZhou:
                    return s2;
                default:
                    return "";
            }
        }

        public static string GetSelectStr_TSJG(string fydm)
        {
            CheckFyType();
            if (string.IsNullOrEmpty(fydm))
            {
                GetSelectStr_TSJG();
            }
            else
            {
                string s1 = string.Format("SELECT * FROM B_TSJG WHERE SFTJFLAG=1 and fy={0} ORDER BY TSPX ASC", fydm);
                //Dim s2 As String = String.Format("SELECT B_TSJG.*,B_TSJG.JGBS as XH FROM B_TSJG WHERE YWBM=1 and fy={0} ORDER BY MC", fydm)
                string s2 = string.Format("SELECT B_TSJG.* FROM B_TSJG  WHERE YWBM=1 and INT(jgbs/65536)={0} ORDER BY MC", fydm);
                

                switch (CurrentFyType)
                {
                    case FyType.Standard:
                        return s1;
                    case FyType.Speciel_LiuZhou:
                        return s2;
                    default:
                        return "";
                }
            }
            return "";

        }

        public static string GetSelectStr_Fy()
        {
            CheckFyType();
            string s1 = "select dm,fjdm,mc from b_fy where fjdm like \'K%\' and (sm is null or sm<>\'已撤销\') order by fjdm";
            string s2 = "select dm,fbdm as fjdm,mc from b_fy where fbdm like \'K%\' and (sm is null or sm<>\'已撤销\') order by fbdm";
            switch (CurrentFyType)
            {
                case FyType.Standard:
                    return s1;
                case FyType.Speciel_LiuZhou:
                    return s2;
                default:
                    return "";
            }


        }
        public static string GetSelectStr_Fy(string dm)
        {
            CheckFyType();
            string s1 = "select dm,fjdm,mc from b_fy where dm=" + dm;
            string s2 = "select dm,fbdm as fjdm,mc from b_fy where dm=" + dm;
            switch (CurrentFyType)
            {
                case FyType.Standard:
                    return s1;
                case FyType.Speciel_LiuZhou:
                    return s2;
                default:
                    return "";
            }


        }

        /// <summary>
        /// 获取下级法院
        /// </summary>
        /// <param name="likeStr"></param>
        /// <param name="fjdm"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string GetSelectStr_Fy(string likeStr, string fjdm)
        {
            CheckFyType();
            string s1;
            string s2;
            if (fjdm == "bfy")
            {
                s1 = string.Format("select dm,fjdm,mc from b_fy where fjdm like \'{0}\' and (sm is null or sm<>\'已撤销\')   order by fjdm", likeStr);
                s2 = string.Format("select dm,fbdm as fjdm ,mc from b_fy where fbdm like \'{0}\' and (sm is null or sm<>\'已撤销\')   order by fbdm", likeStr);
            }
            else
            {
                s1 = string.Format("select dm,fjdm,mc from b_fy where fjdm like \'{0}\' and fjdm<>\'{1}\' and (sm is null or sm<>\'已撤销\')   order by fjdm", likeStr, fjdm);
                s2 = string.Format("select dm,fbdm as fjdm ,mc from b_fy where fbdm like \'{0}\' and fbdm<>\'{1}\' and (sm is null or sm<>\'已撤销\')   order by fbdm", likeStr, fjdm);
            }
            switch (CurrentFyType)
            {
                case FyType.Standard:
                    return s1;
                case FyType.Speciel_LiuZhou:
                    return s2;
                default:
                    return "";
            }


        }



        public static void CheckFyType()
		{
			if (CurrentFyType == FyType.UnKnown)
			{
                IDBService db=(IDBService)new DB2ServiceClass();

                DataTable dt = db.Select("SELECT * FROM B_TSJG WHERE 1<>1", GetConStr());
				
				

				
				if (dt.Columns.Contains("SFTJFLAG"))
				{
					CurrentFyType = FyType.Standard;
				}
				
				if (dt.Columns.Contains("YWBM"))
				{
					CurrentFyType = FyType.Speciel_LiuZhou;
				}
                CurrentFyType = FyType.Speciel_LiuZhou;
			}
		}
        public static string GetConStr()
        {
            return ConfigurationManager.ConnectionStrings["ConDB2SFXXK"].ToString();
        }
    }
}

